from django.core.paginator import Paginator
from django.shortcuts import render

from django_vue_back import settings
from source.models import Source
from utils.customerutils import CustomerUtils
from utils.responsestatus import ResponseStatus
from utils import responsedata


# Create your views here.
def source_upload_token(request):
    key, token = CustomerUtils.create_qiniu_token()
    response = {
        'token': token,
        'key': key,
        'code': ResponseStatus.BUSINESS_SUCCESS
    }
    return CustomerUtils.response_json(response)


def source_page(request):
    if request.method == 'POST':
        form_data = CustomerUtils.get_request_data(request)
        # 分页处理  total  tableData
        source_list = Source.objects.all().order_by('-id')
        # 获取分页对象
        paginator = Paginator(source_list, form_data.get('size', 5))
        # 获取数据总条数
        total = paginator.count
        # 当前页对象
        current_page = paginator.page(form_data.get('page', 1))
        # 当前页数据   [{},{},{}]
        table_data = list(current_page.object_list.values())
        response = {
            'code': ResponseStatus.BUSINESS_SUCCESS,
            'data': {
                'total': total,
                'tableData': table_data
            }
        }
        return CustomerUtils.response_json(response)


def source_add_info(request):
    if request.method == 'POST':
        token = request.headers.get('X-Token')
        # 解析token  获取发布人
        publisher = CustomerUtils.verify_token(token, settings.SECRET).get('username')
        form_data = CustomerUtils.get_request_data(request)
        form_data['publisher'] = publisher
        Source(**form_data).save()
        return CustomerUtils.response_json(responsedata.BUSINESS_SUCCESS_DATA)


def source_remove(request, id):
    from qiniu import Auth
    from qiniu import BucketManager
    access_key = 'u48RMMuTxcgGpA5l9vmZiRypgOYFsUKmAyBVYeic'
    secret_key = 'V6Tvyfz-rN8eyFfxgj6KcVfPR44y7YCXPw1L-oLk'
    # 初始化Auth状态
    q = Auth(access_key, secret_key)
    # 初始化BucketManager
    bucket = BucketManager(q)
    # 你要测试的空间， 并且这个key在你空间中存在
    bucket_name = 'python2511'
    source = Source.objects.filter(id=id).first()
    print(source.key, source.cover_key)
    # 删除bucket_name 中的文件 key
    ret, info = bucket.delete(bucket_name, source.key)
    if source.cover_key:
        bucket.delete(bucket_name, source.cover_key)
    # 删除数据库资源记录
    Source.objects.filter(id=id).delete()
    return CustomerUtils.response_json(responsedata.BUSINESS_SUCCESS_DATA)


# 在线学习 分页
def source_online_page(request):
    if request.method == 'POST':
        form_data = CustomerUtils.get_request_data(request)
        # 分页处理  total  tableData  video
        source_list = Source.objects.filter(file_type='video').order_by('-id')
        # 获取分页对象
        paginator = Paginator(source_list, form_data.get('size', 5))
        # 获取数据总条数
        total = paginator.count
        # 当前页对象
        current_page = paginator.page(form_data.get('page', 1))
        table_data = list(current_page.object_list.values())
        response = {
            'code': ResponseStatus.BUSINESS_SUCCESS,
            'data': {
                'total': total,
                'tableData': table_data
            }
        }
        return CustomerUtils.response_json(response)


# 更新浏览量
def source_online_views(request, id):
    # 获取历史浏览量
    views = Source.objects.filter(id=id).first().views
    # 更新浏览量
    Source.objects.filter(id=id).update(views=views + 1)
    return CustomerUtils.response_json(responsedata.BUSINESS_SUCCESS_DATA)
